package org.techmore.biz;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.techmore.activity.CouponListActivity;
import org.techmore.utils.DatabaseHelper;
import org.techmore.utils.DownloadNoStoreHandler;
import org.techmore.utils.PropertiesUtil;
import org.techmore.vo.AllCouponSeriesVO;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

public class CouponListActivityDataHandler {
	
	private static CouponListActivityDataHandler couponListActivityDataHandler = new CouponListActivityDataHandler();
	
	public static CouponListActivityDataHandler getInstance(){
		return couponListActivityDataHandler;
	}
	
	public List<AllCouponSeriesVO> getCouponList(Context context){
		List<AllCouponSeriesVO> list = new ArrayList<AllCouponSeriesVO>();
		String databaseName = PropertiesUtil.getInstance().getValue("databaseName");
		SQLiteDatabase sqLiteDatabase = null;
		Cursor cursor = null;
		AllCouponSeriesVO allCouponSeriesVO = null;
		String sql ="select * from allCouponSeries";
		try {
			list.clear();
			sqLiteDatabase = new DatabaseHelper(context, databaseName).getReadableDatabase();
			cursor = sqLiteDatabase.rawQuery(sql, new String[]{});
			while(cursor.moveToNext()){
				//couponSeriesNo varchar(24),store varchar(100),subject varchar(100),img48xUrl varchar(200)
				allCouponSeriesVO = new AllCouponSeriesVO();
				allCouponSeriesVO.setCouponSeriesNo(cursor.getString(cursor.getColumnIndex("couponSeriesNo")));
				allCouponSeriesVO.setImg48xUrl(cursor.getString(cursor.getColumnIndex("img48xUrl")));
				allCouponSeriesVO.setStore(cursor.getString(cursor.getColumnIndex("store")));
				allCouponSeriesVO.setSubject(cursor.getString(cursor.getColumnIndex("subject")));
				list.add(allCouponSeriesVO);
			}
		} catch (Exception e) {
			Log.e("sqlite:", e.getMessage());
		}finally{
			cursor.close();
			sqLiteDatabase.close();
		}
//		System.out.println(list.size());
		return list;
	}
	
	

	private SAXParserFactory factory = null;
	private SAXParser parser = null;
	
	private Handler dataHandler = null;
	private ArrayList<AllCouponSeriesVO> list = null;
	public void getCouponListByUrl(final Activity activity, final String url){
		list = new ArrayList<AllCouponSeriesVO>();
		try {
			factory = SAXParserFactory.newInstance();
			parser = factory.newSAXParser();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			Log.e("CouponListActivityDataHandler:", e.getMessage());
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			Log.e("CouponListActivityDataHandler:", e.getMessage());
		}
		
		dataHandler = new Handler(Looper.myLooper()){
    		public void handleMessage(android.os.Message msg) {
    			int sign = msg.what;
    			if(sign == 1){
    				String uri = String.valueOf(msg.obj);
        			try {
						parser.parse(new ByteArrayInputStream(uri.getBytes()), new AllCouponSeriesVO_Handler(list));
						
				        Intent intent = new Intent();
				        intent.putExtra("seachDataUrl", url);
				        intent.putParcelableArrayListExtra("seachData", list);
						intent.setClass(activity, CouponListActivity.class);
						activity.startActivity(intent);
						activity.finish();

					} catch (SAXException e) {
						Log.e("CouponListActivityDataHandler:", e.getMessage());
					} catch (IOException e) {
						Log.e("CouponListActivityDataHandler:", e.getMessage());
					}
    			}
    		};
    	};
    	dataHandler.post(new DownloadNoStoreHandler(url, dataHandler, 1));
	}
	
	
	
	public class AllCouponSeriesVO_Handler extends DefaultHandler{
		private AllCouponSeriesVO allCouponSeriesVO = null;
		private List<AllCouponSeriesVO> allCouponSeriesVOList = null;
		public AllCouponSeriesVO_Handler(List<AllCouponSeriesVO> allCouponSeriesVOList){
			this.allCouponSeriesVOList = allCouponSeriesVOList;
		}
		
		@Override
		public void startDocument() throws SAXException {
			super.startDocument();
		}
		@Override
		public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
			super.startElement(uri, localName, qName, attributes);
			
			if("coupon".equals(qName) || "coupon".equals(localName) ){
				allCouponSeriesVO = new AllCouponSeriesVO();
				allCouponSeriesVO.setCouponSeriesNo(attributes.getValue("couponSeriesNo"));
				allCouponSeriesVO.setImg48xUrl(attributes.getValue("img48xUrl"));
				allCouponSeriesVO.setStore(attributes.getValue("store"));
				allCouponSeriesVO.setSubject(attributes.getValue("subject"));
				allCouponSeriesVOList.add(allCouponSeriesVO);
			}
		}
		@Override
		public void endDocument() throws SAXException {
			super.endDocument();
		}
		@Override
		public void endElement(String uri, String localName, String qName) throws SAXException {
			super.endElement(uri, localName, qName);
			if("coupon".equals(qName) || "coupon".equals(localName)){
				allCouponSeriesVO = null;
			}
		}
		@Override
		public void characters(char[] ch, int start, int length) throws SAXException {
		}
	}

}
